1. Field of the Invention
The present invention relates to a single-chip microcomputer having a CPU and a nonvolatile memory and, more particularly, to a boot region switching method of, when a certain region of a nonvolatile memory is designated as a boot region where a boot program is stored, switching other region to be a new boot region.
2. Description of the Related Art
Single-chip microcomputer (or single-chip micro) is a semiconductor device in which a storage region where a control program is stored and a CPU (central processing unit) for executing operation based on the control program are formed on one semiconductor chip.
In order to facilitate modification of a control program, some of such single-chip microcomputers employ nonvolatile memories including a flash memory as a memory for storing the control program.
Single-chip microcomputer mounted with such a flash memory, even when a control program stored in the flash memory needs to be modified, enables the once stored control program to be rewritten.
Among methods of rewriting such a control program are a writer rewriting mode for rewriting a control program by using a flash writer and a self-programming mode for rewriting a nonvolatile memory at the stage of user's use. The writer rewriting mode is a mode of controlling rewriting of a flash memory by starting instruction execution from a boot memory to communicate with the outside, while a user mode is a mode of executing a user region by similarly booting from a boot memory and then selecting a start address. In the self-programming mode, a boot program and other instructions necessary for rewriting are transferred from a nonvolatile memory or from the outside to a RAM to rewrite the nonvolatile memory based on the instruction read from the RAM. In the self-programming mode, only when rewriting a region containing a boot block, a boot program is required as writing data. A boot program is a program to be executed first at the time of system booting after the system is reset.
At the time of rewriting a certain memory block of a nonvolatile memory by the self-programming mode as described above or at the time of replacing a boot program with a new boot program, a boot region switching process is executed such that a boot program stored in the memory block in question is rewritten into another memory block or a new boot program is stored in another memory block, which causes the region in which the boot program is stored to change to a new boot region.
However, simply transferring a boot program stored in a user region of a certain memory block into a RAM, or erasing an original boot program and storing the boot program read from the RAM in a user region of another memory block might result in a single-chip microcomputer system not being able to continue with normal operation due to instantaneous cut-off of power or the like occurring during erasure of a user region of a nonvolatile memory in which programs, including a boot program (hereinafter referred to as a boot program), are stored. In another case, instantaneous cut-off of power might occur during new program writing after erasure, resulting in incomplete writing. In such cases, even when the system is reset to boot again, there are cases where a boot program has not been normally rewritten to disable re-booting. Therefore, the self-programming mode of rewriting a nonvolatile memory at a stage of user's use has a problem that a user region in which a boot program is stored can not be erased safely.
As a countermeasure which eliminates the problem, techniques for safely rewriting a user region in which a boot program is stored are disclosed in Japanese Patent Laying-Open (kokai) No. Heisei 8-255084 and Japanese Patent Laying-Open (kokai) No. Heisei 10-149282. This patent teaches the process of rewriting a program in a boot area, by copying an old boot program into a free user region to save the same, erasing storage contents of the boot area in question and then writing a new boot program into the area, and after confirming the end of the writing, erasing the old boot program copied into the free user region. Therefore, even when a situation occurs where writing a new program can not be executed completely due to instantaneous cut-off of power or the like, the system can be booted again by the old boot program to prevent the system from entering an unrecoverable state.
With the above-described methods, however, the number of actions is large such as copying of an old boot program into a free area and erasure of an old boot program when it becomes unnecessary, resulting in making procedures complicated and time-consuming.
Conventional single-chip microcomputer which solves the above-described problems is recited in Japanese Patent Laying-Open (kokai) No. 2001-195241.
The conventional single-chip microcomputer is designed to have a boot area designation flag provided for each user region to enable a boot program stored in a nonvolatile memory to be safely rewritten in an onboard mode at a stage of user's use by a less number of actions. Structure of the conventional single-chip microcomputer is shown in FIG. 4.
Boot area designation flag, which is provided for each memory block, is a flag for indicating that a boot program is stored in a user region of the memory block in question.
The conventional single-chip microcomputer includes, as illustrated in FIG. 4, a nonvolatile memory 41, a CPU 2, a boot memory 3, a region switching flag 4 and a RAM 5.
The nonvolatile memory 41, in which programs including a boot program are stored, is divided into two memory blocks 161 and 162. The memory block 161 is formed of a user region A for writing data such as a program code by a user and a region for storing a boot area designation flag A corresponding thereto and the memory block 162 is formed of a user region B and a region for storing a boot area designation flag B corresponding thereto.
The CPU 2 is a main processor which conducts control and operation according to a program stored in the nonvolatile memory 41. The CPU 2 conducts setting of the region switching flag 4 based on the contents of a boot area designation flag. The boot memory 3 has a function of booting the system according to a designated control mode. After the system is booted up, among the programs stored in the nonvolatile memory 41, the boot program is first processed. Rewriting of the boot program is executed by a flash writer when the computer is handled singly by a factory and is executed in a self mode when in an on-board mode at the stage of user's use. The region switching flag 4 has a function of designating a user region in which the boot program is stored at the time of system booting. The RAM 5 temporality stores necessary data in response to an instruction from the CPU 2.
With reference to the flow chart of FIG. 5, description will be made of a procedure of rewriting programs including a boot program in a user region in a self-programming mode in thus structured conventional single-chip microcomputer when the user region A of the memory block 161 is designated for booting (boot area designation flag A=“0”, the boot area designation flag B=“1”). The nonvolatile memory 41 is designed that, when data contents are erased, “1” is stored in all the erased bits.
Here, the user region and the boot area designation flag of the memory block 161 are represented by the user region A and the boot area designation flag A, respectively, and the user region and the boot area designation flag of the memory block 162 are shown as the user region B and the boot area designation flag B, respectively.
The conventional rewriting process involves: first, erasing the storage contents of the user region B and the boot area designation flag B into which a boot program is newly written (Step 201); then, writing the new boot program into the user region B (Step 2002); next, writing data so as to make the boot area designation flag B have boot designation (data “0”) (Step 203).
Then, the storage contents of the user region A and the boot area designation A (Step 204) are erased. At this time, nothing is written to the boot area designation flag A, and thus the data “1” remains the same. The next step is writing with a program other than the boot program into the erased user region A according to the necessity.
In the user mode, when the system is booted by resetting, the program is executed from the boot memory 3, based on which program values of the boot area designation flag A and the boot area designation flag B. These flags are read to determine which of the user region A and the user region B stores the new boot program. The program writes the data of the relevant user region into the region switching flag 4 and the region switching flag 4. The region switching flag 4 is set to read storage contents branched into the user regions in which the new boot program is stored at the time of system booting.
When instantaneous cut-off of power occurs after the end of Step 203 and before Step 204 ends, a plurality of user regions might be designated as a boot region. In such a case, any of the user regions, the user region A, for example, should be preferentially designated as a boot region.
In the conventional single-chip microcomputer, since setting a boot area designation flag and a region switching flag enables a user region including a boot program for booting the system to be switched, thereby enabling boot program rewriting in a nonvolatile memory to be executed always with the old boot program existing, even when rewriting fails to be completed due to instantaneous cut-off of power or the like at the time of rewriting in the self-mode, recovery is possible by further rewriting and rewriting is enabled with a less number of actions in a short period of time.
In such a conventional single-chip microcomputer, however, when instantaneous power cut-off occurs during erasure of the user region B, that is, during the processing of Step 201 in FIG. 5, the value of the boot area designation flag B becomes indefinite to have a valid value depending on situations. At this stage, however, processing of Step 202 for writing the new boot program into the user region B is yet to be completed. Therefore, when the system is reset to boot up, both the boot area designation flags A and B go valid and if setting is made here to preferentially designate the user region B as a boot region, such a problem occurs that a user region in which no boot program is stored might be designated as a boot region.
In a flash memory, in particular, pre-write is conducted of, before erasing written data, once executing writing to make a voltage level held in all the memory cells be the same level. Therefore, when instantaneous power cut-off or the like occurs during pre-write of a certain memory block, a boot area designation flag of the memory block might attain “1” which should be originally “0”, or attain “0” which should be originally “1”.
As a result, in the conventional single-chip microcomputer shown in FIG. 4 as well, such a problem possibly occurs that a memory block in which a boot program is stored can not be specified depending on timing when instantaneous cut-off of power occurs, which disables booting from the boot program. Then, when booting from the boot program is not executed, system operation might not be conducted normally to have erroneous operation.
The above-described conventional single-chip microcomputers have the shortcomings of instantaneous power cut-off or the like occurring during the boot program rewriting processing in the self-programming mode making booting from the boot program impossible.